﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace MMDemo
{
    /// <summary>
    ///当在断点暂停时，委托去修改菜单
    /// </summary>
    public delegate void ChangeMenuFromBreakPoint();
    public partial class SysArithmeticForm : Form
    {
        private Form ParentForm = null;
        private ChangeMenuFromBreakPoint StopMenu = null; 
        public SysArithmeticForm()
        {
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer |
                     ControlStyles.ResizeRedraw |
                     ControlStyles.AllPaintingInWmPaint, true);
            InitializeComponent();
        }
        const int WM_NCLBUTTONDOWN = 0x00A1;
        const int HTCAPTION = 2;
        public bool[] BreakPoint = new bool[50];
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_NCLBUTTONDOWN && m.WParam.ToInt32() == HTCAPTION)
                return;

            base.WndProc(ref m);
        }
        public SysArithmeticForm(Form parent, ChangeMenuFromBreakPoint pauseMenu)
        {
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer |
                     ControlStyles.ResizeRedraw |
                     ControlStyles.AllPaintingInWmPaint, true);
            this.ParentForm = parent;
            this.StopMenu = pauseMenu;
            InitializeComponent();
        }
        private void ArithmeticForm_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            int func = PublicDataClass.functionflag;
            int langvage = PublicDataClass.languageflag;
            string codeurl =Application.StartupPath +  "/code/" + langvage.ToString() + "_" + func.ToString() + ".txt";
            algorithmCtrl1.ReadAlgorithmFromFile(codeurl);        
        }

        /// <summary>
        /// 设置算法框中选中的行
        /// </summary>
        public void SetSelectLine(int line)
        {
            if(line == algorithmCtrl1.GetSelectLine())
            {
                return;
            }

            algorithmCtrl1.SetSelectLine(line);
            algorithmCtrl1.Refresh();
            if (algorithmCtrl1.FindBreakPoint(line) || PublicDataClass.IsSingleStep)
            {
                if (PublicDataClass.IsSingleStep)
                {
                    SysDemoForm.AnimationThread.Suspend();
                }
                else
                {
                    try
                    {
                        ParentForm.Invoke(StopMenu);
                    }
                    catch
                    {
                    }
                }
            }
        }
        public void ClearAllBreakPoint()
        {
            
            algorithmCtrl1.ClearAllBreakPoint();
            algorithmCtrl1.Refresh();
        }
    }
}
